import axios from 'axios'
import type { HouseInfo, HouseListResponse } from '@/mock/house'

// 楼盘基本信息类型
export interface ProjectInfo {
  id: string
  title: string
  subtitle: string
  description: string
  buildingList: BuildingInfo[]
  totalCount: number
}

// 楼栋信息类型
export interface BuildingInfo {
  id: string
  name: string
}

// API 响应基础类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

/**
 * 获取当前楼盘的房源列表
 * @project_id 楼盘id
 * @building_id 楼栋id
 */
export const getHouseList = async ({
  project_id = '',
  building_id = ''
}: {
  project_id: string
  building_id: string
}): Promise<HouseInfo[]> => {
  try {
    const response = await axios.get<HouseListResponse>(
      `/api/house/list?project_id=${project_id}&building_id=${building_id}`
    )

    if (response.data.code === 200) {
      return response.data.data.list
    }
    throw new Error(response.data.message || '获取房源列表失败')
  } catch (error) {
    console.error('获取房源列表失败:', error)
    throw error
  }
}
